package com.xxwy.netty;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
 * @author wy
 * @date 2020/3/31 17:52
 */
public class Server {

  public static void main(String[] args) {
    new Server().start(8000, 100);
  }

  private void start(int beginPort, int count) {
    System.out.println("server starting");
    NioEventLoopGroup bossGroup = new NioEventLoopGroup();
    NioEventLoopGroup workGroup = new NioEventLoopGroup();

    ServerBootstrap serverBootstrap = new ServerBootstrap();
    serverBootstrap.group(bossGroup, workGroup)
        .channel(NioServerSocketChannel.class)
        .childOption(ChannelOption.SO_REUSEADDR, true)
        .childHandler(new ConnectionCountHandler());

    for (int i = 0; i < count; i++) {
      int port = beginPort + i;
      serverBootstrap.bind(port).addListener((ChannelFutureListener) future ->{
        System.out.println("bind success in port " + port);
      });
    }

    System.out.println("service started");

  }
}
